package ffmpeg

import (
	"bytes"
	"net/http"
	"time"

	"gitee.com/linxing_3/sr-core/utils/crypto"
)

func ServeStaticContent(w http.ResponseWriter, r *http.Request, data []byte) {
	setStaticContentCacheControl(w, r)
	w.Header().Set("ETag", GenerateETag(data))

	http.ServeContent(w, r, "", time.Time{}, bytes.NewReader(data))
}
func setStaticContentCacheControl(w http.ResponseWriter, r *http.Request) {
	if r.URL.Query().Has("t") {
		w.Header().Set("Cache-Control", "private, max-age=31536000, immutable")
	} else {
		w.Header().Set("Cache-Control", "no-cache")
	}
}

func GenerateETag(data []byte) string {
	hash := crypto.MD5Bytes(data)
	return `"` + hash + `"`
}

func ServeStaticFile(w http.ResponseWriter, r *http.Request, filepath string) {
	setStaticContentCacheControl(w, r)

	http.ServeFile(w, r, filepath)
}
